﻿// HSS.Data.ModelGenerator.TemplateManager.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       TemplateManager.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Data.ModelGenerator
{
	#region Using Directives
	using System;
	using System.Windows.Forms;
	using HSS.Data.ModelGenerator;
	using HSS.Forms;
	#endregion

	#region TemplateManager
	/// <summary>
	/// Allows the user to manage the collection of Code Templates.
	/// </summary>
	public partial class TemplateManager : Form
	{
		#region Fields
		TemplateData currentProfile;
		bool changesMade;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public TemplateManager()
		{
			InitializeComponent();
			this.Load += new EventHandler(TemplateEditor_Load);
			this.Shown += new EventHandler(TemplateEditor_Shown);
			this.FormClosing += new FormClosingEventHandler(TemplateManager_FormClosing);
		}
		#endregion

		#region Methods
		void RefreshProfiles()
		{
			AppUserConfiguration<TemplateData>.Refresh();
			this.lsbTemplates.DataSource = AppUserConfiguration<TemplateData>.Items;
		}
		void Cancel()
		{
			this.ResetDirty();
		}
		void Save()
		{

			if (string.IsNullOrEmpty(this.txtTemplateName.Text))
			{
				MsgBox.Show("You must provide a Name for the template.");
				return;
			}
			changesMade = true;
			if (null == currentProfile)
			{
				this.currentProfile = new TemplateData()
				{
					Name = this.txtTemplateName.Text,
					Contents = this.txtTemplateContent.Text
				};
				AppUserConfiguration<TemplateData>.Add(this.currentProfile);
			}
			else
			{
				currentProfile.Name = this.txtTemplateName.Text;
				currentProfile.Contents = this.txtTemplateContent.Text;
				AppUserConfiguration<TemplateData>.Save();
			}

			this.ResetDirty();
			this.RefreshProfiles();

		}
		void ResetDirty()
		{
			this.SetButtons();

			this.btnCancel.Enabled = false;
			this.btnSave.Enabled = false;

			this.pnlHeader.Enabled = true;
			this.pnlContent.Enabled = false;
			this.pnlFooter.Enabled = false;

		}
		void SetButtons()
		{
			if (null == currentProfile)
			{
				this.btnDelete.Enabled = false;
				this.btnDuplicate.Enabled = false;
				this.btnEdit.Enabled = false;
			}
			else
			{
				this.btnDelete.Enabled = true;
				this.btnDuplicate.Enabled = true;
				this.btnEdit.Enabled = true;
			}
		}
		#endregion

		#region Event Handlers
		void TemplateEditor_Load(object sender, EventArgs e)
		{
			this.pnlHeader.Enabled = true;
			this.pnlContent.Enabled = false;
			this.pnlFooter.Enabled = false;
		}
		void TemplateEditor_Shown(object sender, EventArgs e)
		{
			this.RefreshProfiles();
		}
		void TemplateManager_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (changesMade)
				this.DialogResult = System.Windows.Forms.DialogResult.OK;
			else
				this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}
		void lsbTemplates_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.currentProfile = (TemplateData)this.lsbTemplates.SelectedItem;
			this.txtTemplateName.Text = this.currentProfile.Name;
			this.txtTemplateContent.Text = this.currentProfile.Contents;
			this.SetButtons();
		}
		void btnNew_Click(object sender, EventArgs e)
		{
			this.currentProfile = null;
			this.txtTemplateContent.Text = Templates.DefaultSingleFileTemplate;
			this.txtTemplateName.Text = string.Empty;

			this.pnlContent.Enabled = true;
			this.pnlFooter.Enabled = true;
			this.pnlHeader.Enabled = false;

		}
		void btnDuplicate_Click(object sender, EventArgs e)
		{
			TemplateData dup = new TemplateData()
			{
				Name = "Copy of - " + this.currentProfile.Name,
				Contents = this.currentProfile.Contents
			};
			this.currentProfile = null;
			AppUserConfiguration<TemplateData>.Add(dup);
			this.ResetDirty();
			this.RefreshProfiles();
		}
		void btnEdit_Click(object sender, EventArgs e)
		{
			if (null != this.currentProfile)
			{
				this.txtTemplateName.Text = this.currentProfile.Name;
				this.txtTemplateContent.Text = this.currentProfile.Contents;

				this.pnlContent.Enabled = true;
				this.pnlFooter.Enabled = true;
				this.pnlHeader.Enabled = false;

			}
		}
		void btnDelete_Click(object sender, EventArgs e)
		{
			if (null != this.currentProfile)
			{
				AppUserConfiguration<TemplateData>.Remove(currentProfile.Name);
				this.RefreshProfiles();
			}
		}
		void btnSave_Click(object sender, EventArgs e)
		{
			this.Save();
		}
		void btnCancel_Click(object sender, EventArgs e)
		{
			this.Cancel();
		}
		void txtTemplateContent_TextChanged(object sender, EventArgs e)
		{
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
		}
		void txtTemplateName_TextChanged(object sender, EventArgs e)
		{
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
		}
		#endregion
	}
	#endregion
}